//
//  BidViewController.m
//  WBBaseProject
//
//  Created by xiaozhi on 2018/3/29.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "BidViewController.h"
#import "BindBankViewController.h"
#import "TenderResultViewController.h"
#import <YYLabel.h>
#import <NSAttributedString+YYText.h>
#import "RechargeViewController.h"
@interface BidViewController ()<UITextFieldDelegate>
{
    //标的id
    NSString *crmOrderId;
    //剩余金额
    NSString *remainingAmount;
    //是否担保
    NSInteger isguarantee;
    //投资人信息id
    NSString *custInfoId;
    //产品id
    NSString *prdId;
    
    //剩余金额
    NSInteger accoutMoney;
    
    //投资金额
    NSInteger currentInvest;
    
    BOOL isRead;
}
@property(nonatomic,strong)UIScrollView* BGView;

//剩余可投金额
@property(nonatomic,strong)UILabel* leftMoneyLb;
//预期金额
@property(nonatomic,strong)UILabel* expectMoneyLb;
//账户余额
@property(nonatomic,strong)UILabel *accoutMoneyLb;
//投资金额
@property(nonatomic,strong)UITextField *investMoney;
//显示最大金额
@property(nonatomic,strong)UILabel *infoLb;

@property(nonatomic,strong)UIView* acountMoneyView;
@property(nonatomic,strong)UIView* investMoneyView;
@property(nonatomic,strong)UIView* bigestMoneyView;
@property(nonatomic,strong)UIView* confiemView;
@property(nonatomic,strong)UIButton *confirmBtn;

@end

@implementation BidViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    self.view.backgroundColor = WB_RGBColor(240, 240, 240);
    self.title = self.data[@"prdName"];
    
    [self leftMoneyLb];
    [self acountMoneyView];
    [self investMoneyView];
    [self bigestMoneyView];
    [self expectMoneyLb];
    [self confiemView];
    
    [self setup];
    [self getData];
}
- (void)setup
{
    crmOrderId = self.data[@"crmOrderId"];
    remainingAmount = self.data[@"remainingAmount"];
    isguarantee = [self.data[@"guarantorStatus"] integerValue];
    custInfoId = self.data[@"custInfoId"];
    prdId = self.data[@"prdId"];
    
    self.leftMoneyLb.text = [NSString stringWithFormat:@"剩余可投金额：%@元",remainingAmount];
    _confirmBtn.enabled = NO;
}

//是否已读
- (void)readClick:(UIButton*)btn
{
    btn.selected = !btn.selected;
    
    isRead = btn.selected;
    if (isRead) {
        
        _confirmBtn.enabled = YES;
    }else{
        _confirmBtn.enabled = NO;
    }
}
//一次性投入最大
- (void)chooseClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        
        currentInvest = accoutMoney;
        [self setInvest];
    }
}
//加减
- (void)bidClick:(UIButton*)btn
{
    if (btn.tag == 101) {
        
        NSLog(@"add");
        currentInvest += 100;
        if (currentInvest >= accoutMoney) {
            currentInvest = accoutMoney;
        }
        
    }else{
        NSLog(@"sub");
        currentInvest -= 100;
        if (currentInvest <= 0) {
            currentInvest = 0;
        }
    }
    [self setInvest];
    [self getExpectMoney];
}
- (void)setInvest
{
    self.investMoney.text = [NSString stringWithFormat:@"%ld",(long)currentInvest];
    [self getExpectMoney];
}
//充值
- (void)toAddMoney
{
//    BindBankViewController *bind = [[BindBankViewController alloc]init];
//    [self.navigationController pushViewController:bind animated:YES];
//    return;
    
    [XYHttpRequest dataRequestWithGetUserInfoSuccess:^(id responseObj) {
        if (Success) {
            if ([responseObj[@"data"][@"user"][@"uxIsSaveDepositCard"] isEqualToString:@"2"]) {
                BindBankViewController *bind = [[BindBankViewController alloc]init];
                [self.navigationController pushViewController:bind animated:YES];
            }else{
                RechargeViewController *vc = [[RechargeViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    } failure:^(NSError *error) {
    }];
}
//确认投标
- (void)confirmClick:(UIButton*)btn
{
    
    [XYHttpRequest dataRequestWithAppDoInvestCrmOrderId:crmOrderId custInfoId:custInfoId prdId:prdId amount:[NSString stringWithFormat:@"%ld",(long)currentInvest] actualAmount:[NSString stringWithFormat:@"%ld",(long)currentInvest] guarantorStatus:isguarantee success:^(id responseObj) {
        
        if (Success) {
            TenderResultViewController *vc = [[TenderResultViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
#pragma mark @"代理"
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    currentInvest = textField.text.integerValue;
    
    if (textField.text.integerValue>accoutMoney) {
        currentInvest = accoutMoney;
    }
    
    [self setInvest];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return [self regularIsAllNumber:string];
}
- (BOOL)regularIsAllNumber:(NSString *)allNumberStr
{
    NSString *pattern = @"^[0-9]*$";
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray *results = [regex matchesInString:allNumberStr options:0 range:NSMakeRange(0, allNumberStr.length)];
    return results.count > 0;
}
#pragma mark 网络请求
- (void)getData
{
    [XYHttpRequest dataRequestWithGetCustBalanaSuccess:^(id responseObj) {
        
        if (Success) {
            accoutMoney = [responseObj[@"data"]integerValue];
            self.accoutMoneyLb.attributedText = [self type:@"账户余额"  money:[NSString stringWithFormat:@"%ld",(long)accoutMoney]];
            self.infoLb.text = [NSString stringWithFormat:@"一次性投入最大金额(%ld元)",(long)accoutMoney];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
}
- (void)getExpectMoney
{
    [XYHttpRequest dataRequestWithGetExceptedReturnCrmOrderId:crmOrderId Amount:self.investMoney.text GuarantorStatus:isguarantee success:^(id responseObj) {
    
        if (Success) {
            self.expectMoneyLb.attributedText =[self type:@"预计收益" money:[NSString stringWithFormat:@"%@",responseObj[@"data"][@"exceptedReturn"]]];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark 视图初始化
- (UIView *)confiemView
{
    if (!_confiemView) {
        _confiemView = [[UIView alloc]init];
        [self.BGView addSubview:_confiemView];
        
        [_confiemView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(0);
            make.height.equalTo(200);
            make.width.equalTo(kScreenWidth);
            make.top.equalTo(self.expectMoneyLb.mas_bottom).offset(0);
            make.bottom.equalTo(-(kScreenHeight-280-200-60+1));
        }];
        
        _confirmBtn=[Factory customButtonWithTitle:@"确认投标"];
        //        UIButton *conf = [[UIButton alloc]init];
        [_confirmBtn addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
        //        [conf setTitle:@"确认投标" forState:UIControlStateNormal];
        //        [conf setBackgroundColor:[UIColor blueColor]];
        //        conf.layer.cornerRadius = 20;
        [_confiemView addSubview:_confirmBtn];
        [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(0);
            make.height.equalTo(60);
            make.width.equalTo(kScreenWidth - 70);
            make.centerX.equalTo(0);
        }];
        
        YYLabel *agreeLb = [[YYLabel alloc] init];
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"已阅读并同意《担保协议》"];
        text.yy_font = [UIFont systemFontOfSize:14];
        text.yy_color = [UIColor lightGrayColor];
        [text yy_setTextHighlightRange:NSMakeRange(6, 6) color:[UIColor blackColor] backgroundColor:[UIColor whiteColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            NSLog(@"点击了");
            
        }];
        agreeLb.attributedText = text;
        [_confiemView addSubview:agreeLb];
        [agreeLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_confirmBtn.top);
            make.centerX.equalTo(20);
            make.height.equalTo(30);
        }];
        
        UIButton *add = [[UIButton alloc]init];
        [add addTarget:self action:@selector(readClick:) forControlEvents:UIControlEventTouchUpInside];
        [add setBackgroundImage:[UIImage imageNamed:@"单选-"] forState:UIControlStateNormal];
        [add setBackgroundImage:[UIImage imageNamed:@"对号"] forState:UIControlStateSelected];
        [_confiemView addSubview:add];
        [add mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(agreeLb.left).equalTo(-5);
            make.width.height.equalTo(20);
            make.centerY.equalTo(agreeLb.centerY);
        }];
    }
    return _confiemView;
}


- (UILabel *)expectMoneyLb
{
    if (!_expectMoneyLb) {
        
        _expectMoneyLb = [[UILabel alloc]init];
        _expectMoneyLb.font = WB_FontSize(15);
        _expectMoneyLb.textColor = [UIColor lightGrayColor];
        _expectMoneyLb.textAlignment = NSTextAlignmentRight;
        _expectMoneyLb.attributedText = [self type:@"预计收益" money:@"0.0"];
        [self.BGView addSubview:_expectMoneyLb];
        
        [_expectMoneyLb mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.BGView);
            make.height.equalTo(30);
            make.width.equalTo(kScreenWidth-15);
            make.top.equalTo(self.bigestMoneyView.mas_bottom).offset(0);
        }];
    }
    return _expectMoneyLb;
}

- (NSMutableAttributedString *)type:(NSString*)type money:(NSString *)money
{
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@：%@元",type,money]];
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(type.length+1, money.length)];
    return text;
}

- (UIView *)bigestMoneyView
{
    if (!_bigestMoneyView) {
        _bigestMoneyView = [[UIView alloc]init];
        _bigestMoneyView.backgroundColor = [UIColor whiteColor];
        [self.BGView addSubview:_bigestMoneyView];
        
        [_bigestMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.BGView).offset(0);
            make.height.equalTo(100);
            make.width.equalTo(kScreenWidth);
            make.top.equalTo(self.investMoneyView.mas_bottom).offset(0);
            
        }];
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = WB_RGBColor(240, 240, 240);
        [_bigestMoneyView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(1);
        }];
        
        UIButton *add = [[UIButton alloc]init];
        [add addTarget:self action:@selector(chooseClick:) forControlEvents:UIControlEventTouchUpInside];
        [add setBackgroundImage:[UIImage imageNamed:@"单选-"] forState:UIControlStateNormal];
        [add setBackgroundImage:[UIImage imageNamed:@"对号"] forState:UIControlStateSelected];
        [_bigestMoneyView addSubview:add];
        [add mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(-15);
            make.width.height.equalTo(25);
            make.centerY.equalTo(0);
        }];
        
        _infoLb = [[UILabel alloc]init];
        _infoLb.textColor = WB_RGBColor(187, 187, 187);
        _infoLb.font = WB_FontSize(15);
        _infoLb.text = @"一次性投入最大金额(30元)";
        [_bigestMoneyView addSubview:_infoLb];
        [_infoLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.right.equalTo(add.mas_left);
            make.top.equalTo(0);
            make.bottom.equalTo(line).offset(0);
        }];
    }
    return _bigestMoneyView;
}

- (UIView *)investMoneyView
{
    if (!_investMoneyView) {
        _investMoneyView = [[UIView alloc]init];
        _investMoneyView.backgroundColor = [UIColor whiteColor];
        [self.BGView addSubview:_investMoneyView];
        [_investMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.BGView).offset(0);
            make.height.equalTo(60);
            make.width.equalTo(kScreenWidth);
            make.top.equalTo(self.acountMoneyView.mas_bottom).offset(0);
            
        }];
        
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = WB_RGBColor(240, 240, 240);
        [_investMoneyView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(1);
        }];
        
        UIButton *add = [[UIButton alloc]init];
        add.tag = 101;
        [add addTarget:self action:@selector(bidClick:) forControlEvents:UIControlEventTouchUpInside];
        [add setBackgroundImage:[UIImage imageNamed:@"icon-copy-拷贝"] forState:UIControlStateNormal];
        [_investMoneyView addSubview:add];
        [add mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(-15);
            make.width.height.equalTo(20);
            make.centerY.equalTo(0);
        }];
        
        _investMoney = [[UITextField alloc]init];
        _investMoney.delegate = self;
        _investMoney.placeholder = @"50元起投";
        _investMoney.font = WB_FontSize(15);
        _investMoney.textAlignment = NSTextAlignmentCenter;
        _investMoney.textColor = [UIColor blackColor];
        _investMoney.keyboardType = UIKeyboardTypePhonePad;
        [_investMoneyView addSubview:_investMoney];
        [_investMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(add.mas_left).offset(-5);
            make.width.equalTo(80);
            make.height.equalTo(40);
            make.centerY.equalTo(0);
        }];
        
        UIButton *sub = [[UIButton alloc]init];
        sub.tag = 100;
        [sub addTarget:self action:@selector(bidClick:) forControlEvents:UIControlEventTouchUpInside];
        [sub setBackgroundImage:[UIImage imageNamed:@"--拷贝"] forState:UIControlStateNormal];
        [_investMoneyView addSubview:sub];
        [sub mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_investMoney.mas_left).offset(-5);
            make.width.height.equalTo(20);
            make.centerY.equalTo(0);
        }];
        
        UILabel *la = [[UILabel alloc]init];
        la.textColor = [UIColor lightGrayColor];
        la.font = WB_FontSize(15);
        la.text = @"投资金额";
        [_investMoneyView addSubview:la];
        [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.right.equalTo(sub).offset(0);
            make.top.equalTo(0);
            make.bottom.equalTo(line).offset(0);
        }];
    }
    return _investMoneyView;
}
- (UIScrollView *)BGView
{
    if (!_BGView) {
        _BGView = [[UIScrollView alloc]init];
        _BGView.showsHorizontalScrollIndicator = NO;
        _BGView.showsVerticalScrollIndicator = NO;
        
        [self.view addSubview:_BGView];
        
        [_BGView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.right.bottom.equalTo(self.view);
            
        }];
    }
    return _BGView;
}

- (UILabel *)leftMoneyLb
{
    if (!_leftMoneyLb) {
        _leftMoneyLb = [[UILabel alloc]init];
        
        _leftMoneyLb.text = @"剩余可投金额：283.00元";
        _leftMoneyLb.font = WB_FontSize(12);
        _leftMoneyLb.textColor = [UIColor lightGrayColor];
        
        [self.BGView addSubview:_leftMoneyLb];
        
        [_leftMoneyLb mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(0);
            make.left.right.equalTo(15);
            make.height.equalTo(30);
        }];
    }
    return _leftMoneyLb;
}
- (UIView *)acountMoneyView
{
    if (!_acountMoneyView) {
        
        _acountMoneyView = [[UIView alloc]init];
        _acountMoneyView.backgroundColor = [UIColor whiteColor];
        [self.BGView addSubview:_acountMoneyView];
        [_acountMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.BGView).offset(0);
            make.height.equalTo(60);
            make.width.equalTo(kScreenWidth);
            make.top.equalTo(self.leftMoneyLb.mas_bottom).offset(0);
            
        }];
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = WB_RGBColor(240, 240, 240);
        [_acountMoneyView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(1);
        }];
        
        UIButton *btn = [[UIButton alloc]init];
        btn.titleLabel.font = WB_FontSize(15);
        [btn setTitle:@"充值" forState:UIControlStateNormal];
        [btn setTitleColor:WB_RGBColor(58, 134, 255) forState:UIControlStateNormal];
        [_acountMoneyView addSubview:btn];
        [btn addTarget:self action:@selector(toAddMoney) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.equalTo(0);
            make.right.equalTo(-15);
            //make.width.equalTo(80);
        }];
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:@"账户余额：0.0元"];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 3)];
        
        _accoutMoneyLb = [[UILabel alloc]init];
        _accoutMoneyLb.textColor = [UIColor lightGrayColor];
        _accoutMoneyLb.font = WB_FontSize(15);
        _accoutMoneyLb.attributedText = text;
        [_acountMoneyView addSubview:_accoutMoneyLb];
        [_accoutMoneyLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(15);
            make.right.equalTo(btn).offset(0);
            make.top.equalTo(0);
            make.bottom.equalTo(line).offset(0);
        }];
    }
    return _acountMoneyView;
}
@end
